﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace Win
{
    public class MainWindowType
    {
        // 输入类型
        public enum InputType : uint
        {
            INPUT_MOUSE = 0x0000,
            INPUT_KEYBOARD = 0x0001,
            INPUT_HARDWARE = 0x0002
        }
        // 鼠标输入结构体
        public struct MouseInput
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public MouseEventFlags dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
        // 鼠标事件标志位
        [Flags]
        public enum MouseEventFlags : uint
        {
            MOUSEEVENTF_MOVE = 0x0001,
            MOUSEEVENTF_LEFTDOWN = 0x0002,
            MOUSEEVENTF_LEFTUP = 0x0004,
            MOUSEEVENTF_RIGHTDOWN = 0x0008,
            MOUSEEVENTF_RIGHTUP = 0x0010,
            MOUSEEVENTF_MIDDLEDOWN = 0x0020,
            MOUSEEVENTF_MIDDLEUP = 0x0040,
            MOUSEEVENTF_XDOWN = 0x0080,
            MOUSEEVENTF_XUP = 0x0100,
            MOUSEEVENTF_WHEEL = 0x0800,
            MOUSEEVENTF_HWHEEL = 0x1000,
            MOUSEEVENTF_MOVE_NOCOALESCE = 0x2000,
            MOUSEEVENTF_VIRTUALDESK = 0x4000,
            MOUSEEVENTF_ABSOLUTE = 0x8000,
            LEFTDOWN = 0x00000002,
            LEFTUP = 0x00000004,
            MIDDLEDOWN = 0x00000020,
            MIDDLEUP = 0x00000040,
            RIGHTDOWN = 0x00000008,
            RIGHTUP = 0x00000010,
            MOVE = 0x00000001,
            WHEEL = 0x00000800,
            ABSOLUTE = 0x00008000
        }
        const int MOUSEEVENTF_WHEEL = 0x800;
        public struct INPUT
        {
            public InputType type;
            public MouseInput mi;
        }
        private const int WHEEL_DELTA = 2000;//783微信高度


        /// <summary>
        /// 移动位置
        /// </summary>
        /// <param name="Distance">要移动的距离（像素）</param>
        public static void Move(int Distance = 20)
        {
            // 获取当前鼠标位置
            Point currentPosition = Cursor.Position;
            // 计算新的位置（向右移动 200 像素）
            int newX = currentPosition.X + Distance;
            int newY = currentPosition.Y;
            // 获取屏幕边界
            Rectangle screenBounds = Screen.PrimaryScreen.Bounds;
            // 检查新位置是否超出屏幕边界
            if (newX > screenBounds.Right)
            {
                newX = screenBounds.Right; // 限制在屏幕右边界
            }
            // 设置鼠标位置
            Cursor.Position = new Point(newX, newY);
        }
        /// <summary>
        /// 移动放行 确定滚动方向（正数为上，负数为下）
        /// </summary>
        /// <param name="Direction">确定滚动方向（正数为上，负数为下）</param>
        public static void Scroll(int Direction)
        {
            // 模拟滚动
            mouse_event(MOUSEEVENTF_WHEEL, 0, 0, (uint)(Direction * WHEEL_DELTA), 0);
            // 添加延迟以模拟更自然的滚动
            Thread.Sleep(10);
        }
        // 导入user32.dll库中的mouse_event函数
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
        /// <summary>
        /// 滚动条模拟
        /// </summary>
        /// <param name="nInputs"></param>
        /// <param name="pInputs"></param>
        /// <param name="cbSize"></param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
        /// <summary>
        /// 快速编辑模式
        /// </summary>
        /// <param name="hConsoleHandle"></param>
        /// <param name="dwMode"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

        public const uint ENABLE_EXTENDED_FLAGS = 0x0080;

        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int X, int Y);



        [DllImport("user32.dll", EntryPoint = "mouse_event")]
        public static extern void MouseEvent(MouseEventFlags dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
        public static void LEFT()
        {
            // 模拟鼠标左键按下
            MouseEvent(MouseEventFlags.LEFTDOWN, 0, 0, 0, UIntPtr.Zero);
            // 模拟鼠标左键抬起
            MouseEvent(MouseEventFlags.LEFTUP, 0, 0, 0, UIntPtr.Zero);
        }
        [DllImport("kernel32.dll", ExactSpelling = true)]
        public static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        const uint SWP_NOSIZE = 0x0001;
        const uint SWP_NOZORDER = 0x0004;
        const uint SWP_SHOWWINDOW = 0x0040;
        public static void Di()
        {
            // 获取控制台窗口句柄
            IntPtr consoleHandle = GetConsoleWindow();

            // 将控制台窗口放置到任务栏最底部
            SetWindowPos(consoleHandle, (IntPtr)0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
        }


    }
}
